<?php namespace simpleFlow\request;

/**
 * Manage requests from web
 * @package simpleFlow
 * @subpackage request
 * @author Guilherme Fontenele
 */
class RequestWeb extends Request
{
	/**
	 * @var array Post
	 */
	protected $arrPost;
	
	/**
	 * Constructor
	 * @param array $arrParams Parameters
	 */
	public function __construct($arrParams = array())
	{
		parent::__construct($arrParams);
		$this->arrPost = $_POST;
		$this->secureGlobals();
	}
	
	/**
	 * Add a post parameter
	 * @param string $param Parameter name
	 * @param id $value Parameter
	 */
	public function addPost($param,$value)
	{
		$this->arrPost[$param] = $value;
	}
	
	/**
	 * Get a post parameter
	 * @param string $param Parameter name
	 * @return id Parameter
	 */
	public function getPost($param)
	{
		return isset($this->arrPost[$param]) ? trim($this->arrPost[$param]) : false;
	}
	
	/**
	 * Clean the Get parameters
	 * @method __secureSuperGlobalGET
	 * @param id $value Parameter
	 * @param string $key Parameter name
	 * @return id Parameter
	 */
	private function __secureSuperGlobalGET(&$value, $key)
	{
		$_GET[$key] = htmlspecialchars(stripslashes($_GET[$key]));
		return $_GET[$key];
	}

	/**
	 * Clean the Post parameters
	 * @method __secureSuperGlobalPOST
	 * @param id $value Parameter
	 * @param string $key Parameter name
	 * @return id Parameter
	 */
	private function __secureSuperGlobalPOST(&$value, $key)
	{
		$_POST[$key] = htmlspecialchars(stripslashes($_POST[$key]));
		return $_POST[$key];
	}
	
	/**
	 * Clean the Get and Post parameters
	 */
	function secureGlobals()
	{
		array_walk($_GET, array($this, '__secureSuperGlobalGET'));
		array_walk($_POST, array($this, '__secureSuperGlobalPOST'));
	}
	
	/**
	 * Get all request parameters
	 * @return RequestWeb self
	 */
	public static function getRequest()
	{
		$arrCalls = explode('/', $_SERVER['REQUEST_URI']);
		$arrParams = array();
		array_shift($arrCalls);

		$arrParams['_protocol_'] = substr($_SERVER['SERVER_PROTOCOL'],0,  strpos($_SERVER['SERVER_PROTOCOL'], '/'));
		
		if(!SF_VIRTUAL_HOST)
		{
			$arrParams['_host_'] = array_shift($arrCalls).'/'.array_shift($arrCalls).'/';
		}else{
			$arrParams['_host_'] = $_SERVER['HTTP_HOST'].'/';
		}
		
		$arrParams['_module_'] = array_shift($arrCalls);
		$arrParams['_app_'] = array_shift($arrCalls);
		$arrParams['_action_'] = array_shift($arrCalls);
		
		for($i=0;$i<count($arrCalls);$i+=2)
		{
			if(trim($arrCalls[$i]))
			{
				$_param = $arrCalls[$i];
				$_value = isset($arrCalls[$i+1]) ? $arrCalls[$i+1] : '';
				$arrParams[$_param] = $_value;
			}
		}
		
		return new self($arrParams);
	}
	
	/**
	 * Get parsed URL
	 * @return string Parsed URL
	 */
	public function getParsedURL()
	{
		$_strHost = $this->strHost;
		if(!SF_VIRTUAL_HOST)
		{
			$_strHost = $_SERVER['HTTP_HOST'].'/'.$_strHost;
		}
		$strURL = strtolower($this->strProtocol)."://{$_strHost}{$this->getParam('_module_')}/{$this->getParam('_app_')}/{$this->getParam('_action_')}";
		foreach($this->getParams() as $_strKey => $_strValue)
		{
			if($_strKey[0] != '_')
			{
				$strURL.= "/{$_strKey}/{$_strValue}";
			}
		}
		return $strURL;
	}
	
	/**
	 * Get URL
	 * @return string URL
	 */
	public function getURL()
	{
		return $_SERVER['REQUEST_URI'];
	}
}

?>